package servlet;

import bao.CustomerDao;
import model.Customer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class CustomerLogin_Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        String sign = req.getParameter("sign");
        String name =  req.getParameter("user");
        String password = req.getParameter("password");
        if(sign == null){
            Customer customer = new Customer(name,password);
            CustomerDao cd = new CustomerDao();
            customer.setU_password(password);
            if(cd.CheckLogin(customer)){
                session.setAttribute("uname",customer.getU_name());
            }else {
                session.setAttribute("msg","用户名或者密码错误！");
            }
            resp.sendRedirect("webpage/Index.jsp");
        }else {
            session.setAttribute("uname",null);
            resp.sendRedirect("webpage/Index.jsp");
        }
    }
}
